package com.itheima.controller;

import com.itheima.common.PageResult;
import com.itheima.common.ResultData;
import com.itheima.domain.SmsHomeNewProduct;
import com.itheima.service.SmsHomeNewProductService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Morty
 * @create 2022 11 12 19:34
 */
@RestController
@CrossOrigin(origins = "*")
public class SmsHomeNewProductController {

    @DubboReference
    SmsHomeNewProductService newProductService;

    //分页查询首页新品
    @GetMapping("/home/newProduct/list")
    public ResultData pageList(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {

        PageResult pageResult = newProductService.pageList(pageNum, pageSize, productName, recommendStatus);
        return ResultData.ok(pageResult, "");
    }

    //添加首页新品
    @PostMapping("/home/newProduct/create")
    public ResultData save(@RequestBody List<SmsHomeNewProduct> homeNewProductList) {
        newProductService.saveBatch(homeNewProductList);
        return ResultData.ok("", "");
    }

    //批量删除首页新品
    @PostMapping("/home/newProduct/delete")
    public ResultData deleteByIds(@RequestParam List<Integer> ids) {
        newProductService.removeByIds(ids);
        return ResultData.ok("", "");
    }

    //批量修改新品状态
    @PostMapping("/home/newProduct/update/recommendStatus")
    public ResultData updateStatusByIds(@RequestParam List<Integer> ids, Integer recommendStatus) {
        for (Integer id : ids) {
            SmsHomeNewProduct newProduct = newProductService.getById(id);
            if (recommendStatus != null) {
                newProduct.setRecommendStatus(recommendStatus);
                newProductService.updateById(newProduct);
            }
        }
        return ResultData.ok("","");
    }
    //批量修改新品动态
    @PostMapping("/home/newProduct/update/sort/{id}")
    public ResultData updateSortById(@PathVariable Integer id,Integer sort){
        SmsHomeNewProduct newProduct = newProductService.getById(id);
        if (sort != null){
            newProduct.setSort(sort);
        }
        newProductService.updateById(newProduct);
        return ResultData.ok("","");
    }
}
